var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onClick: chooseClass
    }
};
var settingCourseTree = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};


var ztree;
var zCoursetree;
function chooseClass(event, treeId, treeNode) {
    console.log(treeNode)
    vm.schoolName=treeNode.name
    vm.schoolId=treeNode.id
    vm.reload()

}

$(function () {
    vm.getCourseTree();
    $("#jqGrid").jqGrid({
        url: baseURL + 'teachPlan/getListBySchoolId?id='+vm.schoolId,
        datatype: "json",
        colModel: [
            // { label: 'id', name: 'id', index: 'id', width: 50, key: true },
            // { label: '用户Id', name: 'userId', index: 'user_id', width: 80 },
            { label: '课程名称', name: 'title', index: 'name', width: 80 },
            { label: '学期', name: 'term', index: 'phone', width: 80 },
            { label: '课时', name: 'classHour', width: 80},
            { label: '是否完结', name: 'isOver', width: 80},
            { label: '授课教师', name: 'teacherName', width: 80 ,
                formatter: function (data) {
                    if(data==null)
                        return "未输入";
                    else
                        return data;

                }},
            { label: '操作', name: 'id', width: 80 ,
                formatter: function (data) {
                    return '<span class="label label-warning" onclick="del('+data+')">删除</span>';

                }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });


})

var vm = new Vue({
    el:'#rrapp',
    data:{
        schoolId:0,
        schoolName:"",
        showList: true,
        title: null,
        teachPlan: {},
        teachers:[],
        menu:{
            parentName:null,
            parentId:0,
            type:1,
            orderNum:0
        }
    },
    methods: {
        courseTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = zCoursetree.getSelectedNodes();
                    if(node[0].id<10000) {
                        //选择上级菜单
                        vm.teachPlan.courseId=node[0].id;
                        vm.teachPlan.title = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        getCourseTree: function(menuId){
            //加载学生树
            $.get(baseURL + "/course/school/select", function(r){
                ztree = $.fn.zTree.init($("#schoolTree"), setting, r.deptList);
                var node = ztree.getNodeByParam("id", 0);
                ztree.selectNode(node);
            })
            //加载课程树
            $.get(baseURL + "/course/menu/select", function(r){
                zCoursetree = $.fn.zTree.init($("#courseTree"), settingCourseTree, r.menuList);
                var node = ztree.getNodeByParam("id", 0);
                zCoursetree.selectNode(node);

                //    vm.menu.parentName = node.name;
            })
            //获取教师列表
            $.get(baseURL + "/course/teacher/getTeacherList", function(r){

                vm.teachers=r.teachers

                //    vm.menu.parentName = node.name;
            })



        },
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.teachPlan = {teacherId:null,
            teacherName:null,
            schoolClassId:null,
            title:null,
            courseId:null};
        },
        update: function (event) {
            var id = getSelectedRows1();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.teachPlan.id == null ? "teachPlan/save" : "teachPlan/update";
            vm.teachPlan.schoolClassId=vm.schoolId
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.teachPlan),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                         //   location. reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/coursebody/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            ('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(baseURL + "sys/coursebody/info/"+id, function(r){
                vm.courseBody = r.courseBody;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                url:baseURL + 'teachPlan/getListBySchoolId?id='+vm.schoolId,
                page:page
            }).trigger("reloadGrid");
    }

    }

});

//选择多条记录
function getSelectedRows1() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
        alert("请选择一条记录");
        return ;
    }

    return grid.getGridParam("selarrrow");
}

function update(item) {
    var id = item;
    if(id == null){
        return ;
    }
    vm.showList = false;
    vm.title = "修改";
    vm.getInfo(id);
}

function del(item) {
    var ids = item;
    if(ids == null){
        return ;
    }

    confirm('确定要删除选中的记录？', function() {
        $.ajax({
            type: "GET",
            url: baseURL + "teachPlan/delete?id="+ids,
            success: function (r) {
                if (r.code == 0) {
                    alert('操作成功');
                    $("#jqGrid").trigger("reloadGrid");

                } else {
                    alert(r.msg);
                }

            }
        });
    });
}